/** ============================================================================================== **/
/**						FISH MANAGER											 				   **/	
/** ============================================================================================== **/

BootApplication.controller("DetFishController",
			[ "$scope",	'$rootScope', '$translate', "NgTableParams", "DetFishService", '$http', 'configuration', "MyTankService", "LoadingAppService", "$mdDialog",
	 function ($scope, 	 $rootScope,   $translate,   NgTableParams,   detFishService,   $http,   configuration,   myTankService,   LoadingAppService,   $mdDialog) {
	
	/** INIZIALIZZAZIONI ********************************************************************************************************************************/
	$scope.fishList						= [];
	$scope.filtriAttivi					= {};
	$scope.displayProp					= {};
	$scope.listFilter 					= [];
	$scope.showBoxApplyFilter			= true;
	$scope.tankList						= [];
	/****************************************************************************************************************************************************/

	/*******************************************************************************************************	GESTIONE FILTER	   ***********	START   */
	$scope.showFilter					= false;
	$scope.hideShowFilter = function () {
		$scope.showFilter = !$scope.showFilter;
	}
	$scope.careLevels 			= [{label:"Moderate"},{label:"Easy"},{label:"Expert"},{label:"Difficult"}];
	$scope.waterFlows 			= [{label:"Strong flowing water"},{label:"Medium flowing water"},{label:"Low flowing water"}];
	$scope.tankLevels 			= [{label:"All"},{label:"Middle-Surface"},{label:"Bottom"},{label:"Bottom-Middle"},{label:"Surface"},{label:"Middle"}];
	$scope.temperaments 		= [{label:"Semi-Aggressive"},{label:"Aggressive"},{label:"Peaceful"}];
	$scope.acquariumTypes 		= [{label:"Cichlid-African"},{label:"Community"},{label:"Ancient-Fish"},{label:"Cichlid-New-World"},{label:"Large-Bottom-Dweller"},{label:"Other-Monster-Fish"}];
	$scope.diets		 		= [{label:"Herbivore"},{label:"Piscivore"},{label:"Omnivore"},{label:"Carnivore"}];
	
	
	$scope.activeFilter = function () {
		console.log("det-fish-ctrl.js		>>>	Applico i filtri");
		/** Nascondo box filtri **/
		$scope.showFilter = false;
		$scope.getFishList();
	}
	
	$scope.removeFilter = function (delFilter) {
		console.log("det-fish-ctrl.js		>>>	Rimuovo i filtri");
		/** Ripulisco la lista dei filtri attivi **/
		$scope.filtriAttivi = [];
		$scope.newListFilter = [];
		$.each( $scope.listFilter, function( index, filter ) {
			if (filter.attr!=delFilter.attr || filter.value!=delFilter.value) { 
				$scope.setFilterObject(filter);
				$scope.newListFilter.push(filter);
			}
		});
		$scope.listFilter=$scope.newListFilter;
		/** Visualizzo/Nascondo i filtri **/
		if ($scope.listFilter.length==0)
			$scope.showFilter = false;
		$scope.getFishList();
	}
	$scope.setFilterObject = function ( filter ){
		if (filter.attr=="commonName") 		$scope.filtriAttivi.commonName = filter.value;
		if (filter.attr=="scientificName") 	$scope.filtriAttivi.scientificName = filter.value;
		if (filter.attr=="origine") 		$scope.filtriAttivi.origin = filter.value;
		if (filter.attr=="origineZone") 	$scope.filtriAttivi.origineZone = filter.value;
		if (filter.attr=="idFamily")	 	$scope.filtriAttivi.idFamily = filter.value;
		if (filter.attr=="careLevel") 		$scope.filtriAttivi.careLevel = filter.value;
		if (filter.attr=="waterFlow") 		$scope.filtriAttivi.waterFlow = filter.value;
		if (filter.attr=="tankLevel") 		$scope.filtriAttivi.tankLevel = filter.value;
		if (filter.attr=="temperament") 	$scope.filtriAttivi.temperament = filter.value;
		if (filter.attr=="acquariumType") 	$scope.filtriAttivi.acquariumType = filter.value;
		if (filter.attr=="diet") 			$scope.filtriAttivi.diet = filter.value;
	}
	/*******************************************************************************************************	GESTIONE FILTER	   ***********	END     */
	
	
	/*******************************************************************************************************	FIGURE VIEW		   ***********	START   */
	$scope.bigCurrentPage 				= 1;
	$scope.itemsPerPage 				= 8;
	$scope.maxSize 						= 5;
	
	$scope.getFishList = function () {
		console.log("det-fish-ctrl.js		>>>	Recupero la lista dei pesci");
		$scope.fishList						= [];
		$scope.displayProp.paginate			= true;
		$scope.displayProp.displayStart		= $scope.bigCurrentPage==1 ? 0 : $scope.itemsPerPage*($scope.bigCurrentPage-1);
		$scope.displayProp.displayLength	= $scope.itemsPerPage;
		detFishService.getFishList( $scope.filtriAttivi, $scope.displayProp ).then(function( response ){
			if (response!=null && response.data.success) {
				$.each( response.data.aaData, function( index, fish ) {
					if (fish.urlImage!=null)
						fish.urlImage = configuration.getParam("Constants").ContextPath + fish.urlImage;
					else 
						fish.urlImage = configuration.getParam("Constants").ContextPath + "img/home/fish/box2-1.jpg";
					$scope.fishList.push(fish);	
				});
				$scope.bigTotalItems 	= response.data.iTotalRecords;
			} else {
				console.log("Errore nella visualizzazione");
			}
			
			$scope.viewActiveFilter();
		});
	}
	$scope.$watch('idUser', function() {
		if ($rootScope.idUser!=undefined) {
			
			var sPageURL 		= decodeURIComponent(window.location.search.substring(1)), sURLVariables = sPageURL.split('&'), sParameterName, i;
			sParameterName 		= sURLVariables[0].split('=');
			$rootScope.tankId 	= sParameterName[1];
			$scope.tankId 		= sParameterName[1];
			
			if ( $rootScope.tankId!=undefined) {
				myTankService.getTank( $rootScope.tankId ).then(function( response ){
					if (response!=null && response.data.success && response.data.aaData!=null) {
						if ( response.data.aaData[0].phMax!=null ) $scope.filtriAttivi.phMax = response.data.aaData[0].phMax;
						if ( response.data.aaData[0].phMin!=null ) $scope.filtriAttivi.phMin = response.data.aaData[0].phMin;
						if ( response.data.aaData[0].ghMax!=null ) $scope.filtriAttivi.ghMax = response.data.aaData[0].ghMax;
						if ( response.data.aaData[0].ghMin!=null ) $scope.filtriAttivi.ghMin = response.data.aaData[0].ghMin;
						if ( response.data.aaData[0].temperatureMax!=null ) $scope.filtriAttivi.temperatureMax = response.data.aaData[0].temperatureMax;
						if ( response.data.aaData[0].temperatureMin!=null ) $scope.filtriAttivi.temperatureMin = response.data.aaData[0].temperatureMin;
					}
					$scope.getFishList();
				});
			} else
				$scope.getFishList();
		}
    });
	
	$scope.setPage = function (pageNo) {
		$scope.bigCurrentPage = pageNo;
		$scope.getFishList();
	};
	$scope.pageChanged = function() {
		$scope.getFishList();
	};
	/*******************************************************************************************************	FIGURE VIEW		   ***********	END     */
	
    $scope.viewActiveFilter = function (){
    	$scope.listFilter = [];
		if ($scope.filtriAttivi.commonName!=undefined && $scope.filtriAttivi.commonName!=""){
			objFilter={};objFilter.attr="commonName";objFilter.value=$scope.filtriAttivi.commonName;$scope.listFilter.push(objFilter);
		}
		if ($scope.filtriAttivi.scientificName!=undefined && $scope.filtriAttivi.scientificName!=""){
			objFilter={};objFilter.attr="scientificName";objFilter.value=$scope.filtriAttivi.scientificName;$scope.listFilter.push(objFilter);
		}
		if ($scope.filtriAttivi.origine!=undefined && $scope.filtriAttivi.origine!=""){
			objFilter={};objFilter.attr="origine";objFilter.value=$scope.filtriAttivi.origine;$scope.listFilter.push(objFilter);
		}
		if ($scope.filtriAttivi.origineZone!=undefined && $scope.filtriAttivi.origineZone!=""){
			objFilter={};objFilter.attr="origineZone";objFilter.value=$scope.filtriAttivi.origineZone;$scope.listFilter.push(objFilter);
		}
		if ($scope.filtriAttivi.idFamily!=undefined && $scope.filtriAttivi.idFamily!=""){
			objFilter={};objFilter.attr="idFamily";objFilter.value=$scope.filtriAttivi.idFamily;$scope.listFilter.push(objFilter);
		}
		if ($scope.filtriAttivi.careLevel!=undefined && $scope.filtriAttivi.careLevel!=""){
			objFilter={};objFilter.attr="careLevel";objFilter.value=$scope.filtriAttivi.careLevel;$scope.listFilter.push(objFilter);
		}
		if ($scope.filtriAttivi.waterFlow!=undefined && $scope.filtriAttivi.waterFlow!=""){
			objFilter={};objFilter.attr="waterFlow";objFilter.value=$scope.filtriAttivi.waterFlow;$scope.listFilter.push(objFilter);
		}
		if ($scope.filtriAttivi.tankLevel!=undefined && $scope.filtriAttivi.tankLevel!=""){
			objFilter={};objFilter.attr="tankLevel";objFilter.value=$scope.filtriAttivi.tankLevel;$scope.listFilter.push(objFilter);
		}
		if ($scope.filtriAttivi.temperament!=undefined && $scope.filtriAttivi.temperament!=""){
			objFilter={};objFilter.attr="temperament";objFilter.value=$scope.filtriAttivi.temperament;$scope.listFilter.push(objFilter);
		}
		if ($scope.filtriAttivi.acquariumType!=undefined && $scope.filtriAttivi.acquariumType!=""){
			objFilter={};objFilter.attr="acquariumType";objFilter.value=$scope.filtriAttivi.acquariumType;$scope.listFilter.push(objFilter);
		}
		if ($scope.filtriAttivi.diet!=undefined && $scope.filtriAttivi.diet!=""){
			objFilter={};objFilter.attr="diet";objFilter.value=$scope.filtriAttivi.diet;$scope.listFilter.push(objFilter);
		}
    }
	
	/*******************************************************************************************************	DETTAGLIO FISH 		   ***********	START	*/
	$scope.activeDetailFish = function(event, fish){
		event.preventDefault();
		$scope.fishModel=fish;
		$scope.showDetailFish=true;
		$scope.showBoxApplyFilter=false;
	}
	$scope.deactiveDetailFish = function(event){
		event.preventDefault();
		$scope.fishModel={};
		$scope.showDetailFish=false;
		$scope.showBoxApplyFilter=true;
	}
	/*******************************************************************************************************	CHANGE TAB 		   ***********	END		*/
	
	$scope.saveLivestock = function( fish ){
		console.log("det-fish-ctrl.js		>>>	Aggiungo il pesce ("+fish.id+") alla vasca ("+$scope.tankId+")");
		detFishService.saveLivestock( $rootScope.tankId, fish ).then(function( response ){
			if (response!=null && response.data.success && response.data.aaData!=null) {
				console.log("det-fish-ctrl.js		>>>	Il pesce ("+fish.id+") e' stato aggiunto alla vasca ("+$scope.tankId+")");
				if ($scope.filtriAttivi.excludeFish==undefined)
					$scope.filtriAttivi.excludeFish = [];
				$scope.filtriAttivi.excludeFish.push(fish.id);
				
				$scope.filtriAttivi.phMax = fish.phMax;
				$scope.filtriAttivi.phMin = fish.phMin;
				$scope.filtriAttivi.ghMax = fish.ghMax;
				$scope.filtriAttivi.ghMin = fish.ghMin;
				$scope.filtriAttivi.temperatureMax = fish.temperatureMax;
				$scope.filtriAttivi.temperatureMin = fish.temperatureMin;
				$scope.getFishList();
			} else {
				console.log("det-fish-ctrl.js		>>>	Il pesce ("+fish.id+") NON e' stato aggiunto alla vasca ("+$scope.tankId+")");
			}
		});
	}
}]);